//
//  CGLView.m
//  gltest
//
//  Created by jiajiawei on 12-12-28.
//  Copyright (c) 2012年 jiajiawei. All rights reserved.
//

#import "CGLView.h"
#import "gl.h"
#import <vector>

@implementation CGLView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        context = NULL;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        context = CGBitmapContextCreate(image_data(), 320, 480, 8, 320 * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        
        CGColorSpaceRelease(colorSpace);

        textureInit();
        
        glLoadIdentifier();
//        glFrustum(-160, 160, -240, 240, 10, 500);
        glFrustum(-1, 1, -1, 1, 0.5, 50);
        glCameraSet(&camera, 0, 0, 0, 0, 0, -1, 0, 1, 0);

        float points[] =
        {
            0.4,  1.5,
            0.6,  1.8,
            0.8,  2.2,
            1.0,  2.8,
            1.2,  3.8,
            1.4,  3.5,
            1.6,  3.0,
            1.8,  3.2,
            2.0,  3.0
        };
        float c[4];
        float pvs[20];
        newtonInterpolation(points, 2, 9, pvs, 20);
        for ( int i = 0; i < 20; i++ )
        {
            NSLog(@"nt = %f", pvs[i]);
        }
        NSLog(@"end");
    }
    return self;
}



void sdrawLine(GLPoint point1, GLPoint point2, int color1, int color2)
{
    BOOL flag = clipEdge(&point1, &point2);
    if ( !flag )
    {
        return;
    }

    glPointNormal(&point1);
    glPointNormal(&point2);
    
    point1.x = 160 + point1.x * 150;
    point1.y = 250 + point1.y * 220;
    point2.x = 160 + point2.x * 150;
    point2.y = 250 + point2.y * 220;

    drawLine(point1.x, point1.y, point2.x, point2.y, color1, color2);
}

void sdrawStriagle(GLPoint point1, GLPoint point2, GLPoint point3)
{
    glPointNormal(&point1);
    glPointNormal(&point2);
    glPointNormal(&point3);

    point1.x = 160 + point1.x * 160;
    point1.y = 240 + point1.y * 240;
    point2.x = 160 + point2.x * 160;
    point2.y = 240 + point2.y * 240;
    point3.x = 160 + point3.x * 160;
    point3.y = 240 + point3.y * 240;
    drawTriagle(point1.x, point1.y, point2.x, point2.y, point3.x, point3.y, 0xFF0000FF, 0xFF00FFFF, 0xFF0000FF);
}

void mdrawStriagle(std::vector<GLPoint> points)
{
    for ( int i = 0; i < points.size(); i++ )
    {
        GLPoint& point = points.at(i);
        point.x = 160 + (point.x / point.w) * 150;
        point.y = 250 + (point.y / point.w) * 220;
    }
    
    drawTSTriagle(points);
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{

    

    return;
    // Drawing code
    glClear();
    memset(image_data(), 0xFF, image_data_size());
    

    GLPoint points[20];
    points[0] = glPointMake(1000, -10, -110, 1);
    points[1] = glPointMake(120, 100, -110, 1);
    points[2] = glPointMake(-120, -120, -120, 1);

    points[0] = glPointMake(-1, -1, -5, 1);
    points[1] = glPointMake(1, -1, -5, 1);
    points[2] = glPointMake(1.0f, 1.0f, -1, 1);
    points[3] = glPointMake(-1, 1.0f, -1, 1);

    points[0].s = 0;
    points[0].t = 0;
    points[1].s = 0.9f;
    points[1].t = 0.0f;
    points[2].s = 0.9f;
    points[2].t = 0.9f;
    points[3].s = 0.0f;
    points[3].t = 0.9f;
    
    float TS[16];
    glMatrixMul(frustumM(), modeviewM(), TS);

    glTransformPoint(TS, &points[0]);
    glTransformPoint(TS, &points[1]);
    glTransformPoint(TS, &points[2]);
    glTransformPoint(TS, &points[3]);

    

    int count = 4;
    clipTriagle(points, &count);
    NSLog(@"n=%d", count);

    std::vector<GLPoint> pts;
    for ( int i = 0; i < count; i++ )
    {
        pts.push_back(points[i]);
        //glPointNormal(&pts.back());
        if ( pts.back().x > 1.1 || pts.back().y > 1.1 )
        {
            NSLog(@"test");
        }
    }
    mdrawStriagle(pts);


    points[0] = glPointMake(-1, -1, -1, 1);
    points[1] = glPointMake(1, -1, -1, 1);
    points[2] = glPointMake(1.0f, 1.0f, -10, 1);
    points[3] = glPointMake(-1, 1.0f, -10, 1);

    points[0].s = 0;
    points[0].t = 0;
    points[1].s = 0.9f;
    points[1].t = 0.0f;
    points[2].s = 0.9f;
    points[2].t = 0.9f;
    points[3].s = 0.0f;
    points[3].t = 0.9f;


    glTransformPoint(TS, &points[0]);
    glTransformPoint(TS, &points[1]);
    glTransformPoint(TS, &points[2]);
    glTransformPoint(TS, &points[3]);



    count = 4;
    clipTriagle(points, &count);
    NSLog(@"n=%d", count);

    pts.clear();
    for ( int i = 0; i < count; i++ )
    {
        pts.push_back(points[i]);
        //glPointNormal(&pts.back());
        if ( pts.back().x > 1.1 || pts.back().y > 1.1 )
        {
            NSLog(@"test");
        }
    }
    mdrawStriagle(pts);

    
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, 320, 480), cgImage);
    CGImageRelease(cgImage);
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    glLoadIdentifier();
    glLoadMatrix(camera.CT);
    firstPoint = [[touches anyObject] locationInView:self];
    [self setNeedsDisplay];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    glLoadIdentifier();
    
    CGPoint currPoint = [[touches anyObject] locationInView:self];
    //glCameraSlide(&camera, currPoint.x - firstPoint.x, currPoint.y - firstPoint.y, 0);

    //glCameraRoll(&camera, currPoint.y - firstPoint.y);
    
    glLoadMatrixFromCamera(&camera);
    //glTranslate(currPoint.x - firstPoint.x, currPoint.y - firstPoint.y, 0);
    glTranslate(0, 0, -5);
    glRotate(currPoint.x - firstPoint.x, 1, 1, 0);
    glTranslate(0, 0, 5);
    //firstPoint = currPoint;
    [self setNeedsDisplay];
}

@end
